<?php
/**
 * @ClassName: Index
 * @Description: TODO
 * @author: wyaohu
 * @Date: 2024-09-14 11:02
 * @Version: V1.0.0
 */

namespace app\api\controller;

use app\common\model;
use think\Controller;

class Index extends Controller
{
    public function index()
    {

    }

    /**
     * 获取主题
     * @return \think\response\Json
     */
    public function getTheme()
    {
        $data = model\Theme::where(['status' => 1])->find();
        if ($data) {
            $data['bg_pic'] = replace_url($data['bg_pic']);
        }

        return json([
            'code' => 1,
            'data' => $data,
        ]);
    }

    /**
     * 获取系统信息
     * @return \think\response\Json
     */
    public function getSystemInfo()
    {
        $data = sys_config('web_config');

        return json([
            'code' => 1,
            'data' => $data,
        ]);
    }

    /**
     * 获取引导图
     * @return \think\response\Json
     */
    public function appGuide()
    {
        $swiper = model\CmsBanner::where(['position_id' => 2, 'is_show' => 1])
//            ->cache('appGuide', mt_rand(7000, 8000))
            ->order('sort')
            ->field('id,title,link,image')
            ->select();

        foreach ($swiper as $key => $value) {
            $swiper[$key]['image'] = replace_url($value['image']);
        }

        return json([
            'code' => 1,
            'data' => $swiper,
        ]);
    }

    /**
     * 获取APP版本信息
     * @return \think\response\Json
     */
    public function getAppVersion()
    {
        $appType = input('appType', 'user');
        $systemType = input('type', 'android');
        $info = model\AppVersion::where("type", $systemType)->where('app_type', $appType)
            ->field("app_type,type,new_version,download_url,qiniu_url,update_text")->order('create_time desc')->find();

        if ($info) {
            return json([
                'code' => 1,
                "info" => $info,
            ]);
        } else {
            return json([
                'code' => 0,
                "info" => '没有版本号信息',
            ]);
        }
    }

    protected function getCmsBanner($position_id = 1, $cache_key = "cache_key", $isList = 1)
    {
        $where = [];
        $where[] = ["position_id", '=', $position_id];
        $where[] = ["is_show", '=', 1];

        $field = "id,title,link,image";

        if ($isList) {
            $advList = model\CmsBanner::where($where)
                ->cache($cache_key, 60)
                ->order('sort')
                ->field($field)
                ->select();
        } else {
            $advList = model\CmsBanner::where($where)
                ->cache($cache_key, 60)
                ->order('sort')
                ->field($field)
                ->find();
        }

        return $advList;
    }

}